package com.xinqi.modules.databank.v2.service.sync.impl;

import com.xinqi.common.base.enums.YesNoNumberEnum;
import com.xinqi.common.core.enums.AssistantMessageTypeEnum;
import com.xinqi.common.core.enums.CourseContentTypeEnum;
import com.xinqi.common.core.enums.CourseResourseSynStatusEnum;
import com.xinqi.modules.course.content.dto.client.CourseContentClient;
import com.xinqi.modules.course.content.dto.req.CourseContentCreateDTO;
import com.xinqi.modules.databank.dto.req.CourseResourcePartQueryDTO;
import com.xinqi.modules.databank.v2.convert.CourseResourceConvert;
import com.xinqi.modules.databank.v2.domain.CourseResourceEntity;
import com.xinqi.modules.databank.v2.manager.CourseResourceBaseManager;
import com.xinqi.modules.databank.v2.service.assistant.AssistantListen;
import com.xinqi.modules.databank.v2.service.student.ResourceStudentLogService;
import com.xinqi.modules.databank.v2.service.sync.CourseResourceSyncService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 同步信息
 * @author dzy
 */
@Service
@RequiredArgsConstructor
public class CourseResourceSyncServiceImpl implements CourseResourceSyncService {

    private final CourseResourceConvert courseResourceConvert;

    private final CourseContentClient courseContentClient;

    private final CourseResourceBaseManager resourceBaseManager;

    private final ResourceStudentLogService resourceStudentLogService;

    private final AssistantListen assistantListen;

    /**
     * 同步主表信息
     * @param entity 资料实体
     */
    @Override
    public void syncResourceContent(CourseResourceEntity entity) {
        CourseContentCreateDTO contentCreateDTO = new CourseContentCreateDTO();
        contentCreateDTO.setUserId(entity.getUserId());
        contentCreateDTO.setContentId(entity.getId());
        contentCreateDTO.setTitle(entity.getTitle());
        contentCreateDTO.setContent(entity.getContent());
        contentCreateDTO.setType(entity.getContentType());
        contentCreateDTO.setClassId(entity.getClassId());
        contentCreateDTO.setChapterId(entity.getChapterId());
        contentCreateDTO.setStatus(entity.getStatus());
        contentCreateDTO.setReleaseTime(entity.getReleaseTime());
        contentCreateDTO.setEndTime(entity.getEndTime());
        courseContentClient.saveContent(contentCreateDTO);
    }

    /**
     * 同步班级
     * @param entity 资料实体
     */
    @Override
    public List<CourseResourceEntity> syncClass(CourseResourceEntity entity) {
        List<CourseResourceEntity> resourceList = new ArrayList<>();
        if (entity.getSynchro() != null && Objects.equals(entity.getSynchro(), CourseResourseSynStatusEnum.SYN.getCode())) {
            if (StringUtils.isNotEmpty(entity.getClassIds())) {
                CourseResourcePartQueryDTO query = new CourseResourcePartQueryDTO();
                query.setContentId(entity.getId());
                String[] classIds = entity.getClassIds().split(",");
                for (int i = 0; i < classIds.length; i++) {
                    CourseResourceEntity newEntity = courseResourceConvert.newEntity(entity);
                    newEntity.setId(null);
                    newEntity.setChapterId(null);
                    newEntity.setClassIds(null);
                    newEntity.setClassId(Long.valueOf(classIds[i]));
                    resourceList.add(newEntity);
                }
                resourceBaseManager.saveBatch(resourceList);
                // 已发布资料同步对应数据
                if (Objects.equals(YesNoNumberEnum.YES.getCode(), entity.getStatus())) {
                    // 同步学生数据
                    resourceStudentLogService.syncBatchStudentLog(resourceList);
                    // 同步更新主表信息
                    this.saveBatchResourceContent(resourceList);
                    // 课程助手发布消息
                    resourceList.forEach(item -> {
                        if (Objects.equals(CourseContentTypeEnum.DATA.getCode(), item.getContentType())) {
                            assistantListen.releaseCourseResource(item.getId(), AssistantMessageTypeEnum.DATA);
                        }else if (Objects.equals(CourseContentTypeEnum.INTERACTIVE_VIDEO.getCode(), item.getContentType())) {
                            assistantListen.releaseCourseResource(item.getId(), AssistantMessageTypeEnum.INTERACTIVE_VIDEO);
                        }
                    });
                }
            }
        }
        return resourceList;
    }

    /**
     * 批量更新课程内容汇总数据
     * @author dzy
     */
    private void saveBatchResourceContent(List<CourseResourceEntity> resourceList) {
        List<CourseContentCreateDTO> list = new ArrayList<>();
        resourceList.forEach(entity -> {
            CourseContentCreateDTO contentCreateDTO = new CourseContentCreateDTO();
            contentCreateDTO.setUserId(entity.getUserId());
            contentCreateDTO.setContentId(entity.getId());
            contentCreateDTO.setTitle(entity.getTitle());
            contentCreateDTO.setContent(entity.getContent());
            contentCreateDTO.setType(entity.getContentType());
            contentCreateDTO.setClassId(entity.getClassId());
            contentCreateDTO.setChapterId(entity.getChapterId());
            contentCreateDTO.setStatus(entity.getStatus());
            contentCreateDTO.setReleaseTime(entity.getReleaseTime());
            contentCreateDTO.setEndTime(entity.getEndTime());
            list.add(contentCreateDTO);
        });
        courseContentClient.saveBatchContent(list);
    }

}
